package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 朱峰
 * @date 2022-2-10 9:40
 */
public class 存在重复元素_217 {
    public boolean containsDuplicate1(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            int defNum = map.getOrDefault(num, 0);
            if (defNum > 0) {
                return true;
            }
            map.put(num, defNum + 1);
        }
        return false;
    }

    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (!set.add(num)) {
                return true;
            }
        }
        return false;
    }

    @Test
    public void testContainsDuplicate () {
        int[] nums = {1,2,3,4};
        System.out.println(containsDuplicate(nums));
    }
}
